import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Vector;

import javax.swing.JPanel;
import javax.swing.Timer;

import shapes.Shape;
import trajectories.Directions;

public class Field extends JPanel implements ActionListener {

    private final List<Shape> shapes = new Vector<Shape>();

    public void add(Shape s) {
        shapes.add(s);
        repaint();
    }

    public void clear() {
        shapes.clear();
        repaint();

    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        g.drawLine(100, 5, 100, 340);
        g.drawLine(100, 5, 430, 5);
        g.drawLine(430, 5, 430, 340);
        g.drawLine(430, 340, 100, 340);
        for (Shape s : shapes) {
            s.paintComponent(g);
        }

    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(440, 370);

    }

    public void moveShapes() {
        Timer timer = new Timer(10, this);
        timer.setRepeats(true);
        timer.start();

    }

    @Override
    public void actionPerformed(ActionEvent e) {
        for (Shape s : this.shapes) {
            s.move();
            checkShapesJogging();
            repaint();
        }

    }

//    private void checkShapesJogging() {
//        for (Shape shape : shapes) {
//            Iterator<Shape> iter = shapes.iterator();
//            if (iter.hasNext()) {
//                iter.next();
//            }
//            while (iter.hasNext()) {
//                Shape nextShape = iter.next();
//                if (shape.xPos == nextShape.xPos + Shape.getWidth()) {
//                    if (shape.yPos + Shape.getHeight() < nextShape.yPos) {
//
//                        continue;
//                    }
//                    if (shape.yPos > nextShape.yPos + Shape.getHeight()) {
//
//                        continue;
//                    }
//                    changeShapesDirection(shape, nextShape);
//                }
//
//                if (shape.xPos + Shape.getWidth() == nextShape.xPos) {
//                    if (nextShape.yPos + Shape.getHeight() < shape.yPos) {
//
//                        continue;
//                    }
//                    if (shape.yPos + Shape.getHeight() < nextShape.yPos) {
//
//                        continue;
//                    }
//                    changeShapesDirection(shape, nextShape);
//                }
//
//            }
//        }
//
//    }
    private void checkShapesJogging() {
        for (int firstIndex = 0; firstIndex < shapes.size() - 1; firstIndex++) {
            for (int secondIndex = firstIndex + 1; secondIndex < shapes.size(); secondIndex++) {
                Shape firstShape = shapes.get(firstIndex);
                Shape secondShape = shapes.get(secondIndex);
                if (firstShape.xPos == secondShape.xPos + Shape.getWidth()) {
                    if (firstShape.yPos + Shape.getHeight() < secondShape.yPos) {
                        continue;
                    }
                    if (firstShape.yPos > secondShape.yPos + Shape.getHeight()) {
                        continue;
                    }
                    changeShapesDirection(firstShape, secondShape);
                }
                if (firstShape.xPos + Shape.getWidth() == secondShape.xPos) {
                    if (secondShape.yPos + Shape.getHeight() < firstShape.yPos) {
                        continue;
                    }
                    if (firstShape.yPos + Shape.getHeight() < secondShape.yPos) {
                        continue;
                    }
                    changeShapesDirection(firstShape, secondShape);
                }
            }
        }
    }

    private void changeShapesDirection(Shape shape, Shape nextShape) {
        if (shape.getTrajectoryDirection().equals(Directions.RIGHT)) {
            shape.setTrajectoryDirection(Directions.LEFT);
            nextShape.setTrajectoryDirection(Directions.RIGHT);
        } else {
            shape.setTrajectoryDirection(Directions.RIGHT);
            nextShape.setTrajectoryDirection(Directions.LEFT);
        }
    }

}
